package ca.etsmtl.gti785;

import java.util.ArrayList;
import ca.etsmtl.gti785.classes.Application;
import ca.etsmtl.gti785.classes.Server;
import ca.etsmtl.gti785.util.ServerAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * This is the activity used for selecting a server receiver for the selected file.
 * @author Étienne Faucher, Pierre-Mary Bien-Aimé
 */
public class ServerSelectionActivity extends Activity 
{
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity2_main);

		ArrayList<Server> lstServeursReceveurs = new ArrayList<Server>();
		for(Server item:Application.getInstanceOf().getServers())
		{
			if(!item.IsSelected())
			{
				lstServeursReceveurs.add(item);
			}
		}
	
		final ListView serverView = (ListView) findViewById(R.id.server_list);
		final ServerAdapter adapter = new ServerAdapter(this, R.layout.server_layout, lstServeursReceveurs);			
		serverView.setAdapter(adapter);
		serverView.setOnItemClickListener(new OnItemClickListener() 
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
			{
				final Server serveurReceveur = (Server) parent.getItemAtPosition(position);
				
				Server serverSource = Application.getInstanceOf().getSelectedServer();
				serverSource.SendFiles(serveurReceveur, Application.getInstanceOf().getSelectedFichier());
				
				Toast.makeText(getApplicationContext(), getApplicationContext().getResources()
						.getString(R.string.file_transfer_started), Toast.LENGTH_LONG).show();
				
				setResult(Activity.RESULT_OK);
				finish();
			} 
		});		
	}
}
